import vue from 'vue'
import Vuex from 'vuex'
import router,{resetRouter} from "../router";
import createPersistedState from 'vuex-persistedstate'
vue.use(Vuex)


function addNewRouter(menulist) {
    // {
    //     path:'/Supplier',
    //         name:'supplier',
    //     meta:{
    //     title:'商家管理'
    // },
    //     component:()=>import('../components/supplier/SupplierManage.vue'),
    // },
    let routes = router.options.routes
    routes.forEach(routeItem=>{
        if(routeItem.path=="/Index"){
            menulist.forEach(menu=>{
                let childRoute = {
                        path:'/'+menu.menuclick,
                        name:'menu.menuname',
                        meta:{
                        title:'menu.menuname'
                    },
                        component:()=>import('../components/'+menu.menucomponent),
                }
                routeItem.children.push(childRoute)
            })
        }
    })
    resetRouter()
    router.addRoutes(routes)
}

export default new Vuex.Store({
    state:{
        menu:[]
    },
    mutations:{
        setMenu(state,menulist) {
            state.menu = menulist

            addNewRouter(menulist)
        },

        updateName(state,name){
            state.name = name
        }
    },
    getters:{
        getMenu(state){
            return +state.menu
        }
    },
    plugins:[createPersistedState()]
})
